﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WpfWordsLearner.Helper;

namespace WpfWordsLearner.ViewModels
{
	public class TestViewModel : BaseViewModel
	{
		Queue<Model.TestWordModel> m_answers = null;
		List<Model.TestWordModel> m_resultOfTest = null;
		public TestViewModel()
		{
			m_answers = new Queue<Model.TestWordModel>();
			Result = new List<Model.TestWordModel>();
			foreach (var item in WordsManager.Instance.GetWordsList())
			{
				m_answers.Enqueue(new Model.TestWordModel() { Word = item, Answer = "Enter answer here...", IsCorrect = false });
			}
			if (m_answers.Count > 0)
			{
				IsNextAvaliable = true;
				IsResultAvaliable = false;
				CurrentAnswer = m_answers.Dequeue();
			}
		}

		public List<Model.TestWordModel> Result
		{
			get { return m_resultOfTest; }
			set
			{
				m_resultOfTest = value;
				OnPropertyChanged(() => Result);
			}
		}

		private bool m_isNext = false;
		public bool IsNextAvaliable
		{
			get
			{
				return m_isNext;
			}
			set
			{
				m_isNext = value;
				OnPropertyChanged(() => IsNextAvaliable);
			}
		}

		private bool m_isResultAvaliable = false;
		public bool IsResultAvaliable
		{
			get
			{
				return m_isResultAvaliable;
			}
			set
			{
				m_isResultAvaliable = value;
				OnPropertyChanged(() => IsResultAvaliable);
			}
		}

		private Model.TestWordModel m_answer = null;
		public Model.TestWordModel CurrentAnswer
		{
			get { return m_answer; }
			set
			{
				m_answer = value;
				OnPropertyChanged(() => CurrentAnswer);
			}
		}

		public ICommand NextCommand { get { return new BaseCommand(Click, new Func<bool>(CanNextExecute)); } }
		private void Click()
		{
			if (CurrentAnswer != null)
			{
				if (CurrentAnswer.Word.Translate.ToLower().Contains(CurrentAnswer.Answer.ToLower().Trim()))
				{
					CurrentAnswer.IsCorrect = true;
				}
				Result.Add(CurrentAnswer);
			}
			CurrentAnswer = m_answers.Dequeue();
		}

		private bool CanNextExecute()
		{
			bool result = (m_answers != null && m_answers.Count > 0);
			IsResultAvaliable = !result;
			IsNextAvaliable = result;
			//if (IsResultAvaliable)
			//ResultCommand.Execute(null);
			return result;
		}

		public ICommand ResultCommand { get { return new BaseCommand(Click2); } }
		private void Click2()
		{
			System.Windows.Window wind = App.Current.Windows.Cast<System.Windows.Window>().SingleOrDefault(x => x.IsActive);
			if (wind != null)
				wind.Close();

			Views.ResultView result = new Views.ResultView();
			result.dgrResults.DataContext = this;
			result.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
			result.Topmost = true;
			result.ShowInTaskbar = false;
			result.Show();

		}

		//private bool CanResultExecute()
		//{
		//    bool result = (m_answers != null && m_answers.Count == 0);
		//    if (result)
		//        IsResultAvaliable = true;
		//    return result;
		//}

	}
}
